const global = getApp().globalData;
const wxApi = global.wxApi;
const getUserInfo = global.getUserInfo;
const Api = global.Api;
const util = global.util;

const attentionModes = require( '../../config/attentionModes' );
console.log( attentionModes );
Page({
  data: {
    modes: attentionModes,
    activeMode: null,
    levels: [],
    activeLevel: 2,
    screenH: 0,
    ranks: [],
    myGrade: null,
    user: null,
    scrollTop: 0
  },
  onLoad( options ) {
    wxApi.setNavTitle( '排行榜' );
    let modeIndex = options.modeIndex;
    this.setActiveMode( attentionModes[ modeIndex ] );
    this.init();
  },
  init() {
    this.pageNo = 1;
    this.loadLevels();
    this.refreshRanks();
    this.isLoadingMore = false;
    let user = getUserInfo();
    console.log( user );
    this.setData({
      user: user
    })
    wxApi.getScreenSize( size => {
      console.log( size );
      this.setData({
        screenH: size.height
      })
    })
  },
  /**
   * 选择模式
   */
  onTapMode( e ) {
    let mode = e.currentTarget.dataset.mode;
    this.setActiveMode( mode );
    this.onConditionsChange();
    console.log( mode );
  },
  /**
   * 选择级别
   */
  onTapLevel( e ) {
    let level = e.currentTarget.dataset.level;
    this.setActiveLevel( level );
    this.onConditionsChange();
  }, 
  /**
   * 条件发生变化
   */
  onConditionsChange() {
    this.refreshRanks();
  },
  /**
   * 滚动到底部
   */
  onScrollBottom() {
    this.loadMoreRanks();
  },
  setActiveMode( mode ) {
    this.setData({
      activeMode: mode
    })
  },
  setActiveLevel( level ) {
    this.setData({
      activeLevel: level
    })
  },
  setRanks( ranks ) {
    this.setData({
      ranks: ranks
    })
  },
  setScrollTop( val ) {
    this.setData({
      scrollTop: val
    })
  },
  loadLevels() {
    let arr = [];
    for ( let i = 2; i <= 9; i ++ ) {
      arr.push( i );
    }
    this.setData({
      levels: arr,
      activeLevel: arr[0]
    })
  },
  refreshRanks() {
    this.pageNo = 1;
    this.loadMyGrade();
    this.setScrollTop( 0 );
    this.loadRankList( 1, ranks => {
      console.log( ranks );
      this.setRanks( ranks );
    });
  },
  loadMoreRanks() {
    if ( this.isLoadingMore ) {
      return;
    }
    this.isLoadingMore = true;
    this.pageNo ++;
    let totalRanks = this.data.ranks;
    this.loadRankList( this.pageNo, ranks => {
      
      if ( ranks.length ) {
        totalRanks = totalRanks.concat( ranks );
        this.setRanks( totalRanks );
        
      } else {
        this.pageNo --;
      }
      this.isLoadingMore = false;
    });
  },
  loadMyGrade() {
    let activeMode = this.data.activeMode;
    let activeLevel = this.data.activeLevel;
    let params = {
      mode: activeMode.id,
      size: activeLevel
    }
    Api.getMyGrade( params, grade => {
      console.log( grade );
      grade.myBestText = this.timeFormat( grade.myBest );
      this.setData({
        myGrade: grade
      })
    })
  },
  /**
   * 加载排行榜数据
   * @param {*} pageNo
   */
  loadRankList( pageNo, fn ) {
    let activeMode = this.data.activeMode;
    let activeLevel = this.data.activeLevel;
    let params = {
      mode: activeMode.id,
      size: activeLevel,
      pageNo: pageNo || 1
    }
    Api.getRankList( params, res => {
      fn && fn( res.records );
    } )
  },
  timeFormat( msec ) {
    let time = util.msecFormat( msec );
    return time.minute + ':' + time.second + '.' + time.ms;
  }
}) 